<?php

function getDomain()
{
    return $_SERVER['SERVER_NAME'];
}

/**
 * 格式化输出函数
 *
 * @param mixed   $var
 * @param boolean $exit
 */
function pr($var, $exit = false)
{
    echo '<pre style="font-size:16px; color:#0000FF">';
    if (is_array($var)){
        print_r($var);
    }else if(is_object($var)){
        echo get_class($var)." Object";
    }else if(is_resource($var)){
        echo (string)$var;
    }else{
        echo var_dump($var);
    }
    echo '</pre>';
    if ($exit) exit;
}


/**
 * 取$from~$to范围内的随机数
 * @param $from	下限
 * @param $to   上限
 * @return unknown_type
 */
function random_from_to($from,$to)
{
	$size = $from-$to;//数值区间
	$max  = 30000;//最大
	if($size < $max){
		return $from + rand(0,$size);
	}
	else{
		if($size % $max){
			return $from + random_from_to(0,$size / $max) * $max + rand(0,$size % $max);
		}
		else{
			return $from + random_from_to(0,$size / $max) * $max + rand(0,$max);
		}
	}	
}

function add_slashes($string)
{
    if(!$GLOBALS['magic_quotes_gpc']) {
        if(is_array($string)) {
            foreach($string as $key => $val){
                $string[$key] = add_slashes($val);
            }
        }
        else{
            $string = addslashes($string);
        }
    }
    return $string;
}

if (!function_exists("htmlspecialchars_decode")) 
{
	function htmlspecialchars_decode($string, $quote_style = ENT_COMPAT) {
	return strtr($string, array_flip(get_html_translation_table(HTML_SPECIALCHARS, $quote_style)));
	}
}


//时间处理区
function TimeFomat($time){
	$hour=date('g', strtotime($time));
	$ampm=date('a', strtotime($time));
	$time=date('Y年m月d日', strtotime($time));
	switch ($ampm){
	case "pm":
		$text=pm($hour);
		break;
	case "am":
		$text=am($hour);
		break;
	}
	echo $time.$text.$hour."点";

}

function get_day_of_week()
{
    $now = time();
    $dow = strftime('%u', $now);
    return intval($dow);
}

function timetips(){
	$hour=date('H');
	switch ($hour){
		case  $hour <= 4 :
			$text='凌晨';break;		
		case  $hour <= 8 :
			$text='早晨';break;		
		case  $hour < 12 :
			$text='早上';break;
		case  $hour = 12 :
			$text='深夜';break;		
		case  $hour <= 5 :
			$text='下午';break;		
		case  $hour <= 7 :
			$text='黄昏';break;		
		case  $hour <= 11 :
			$text='晚';break;		
		case  $hour = 12 :
			$text='中午';break;		
	}
	return $text;
}



function CsubStr($str,$start,$end,$sss='...') 
{
    /*
    UTF-8 version of substr(), for people who can't use mb_substr() like me.
    Length is not the count of Bytes, but the count of UTF-8 Characters
    Author: Windix Feng
    Bug report to: windix(AT)263.net, http://www.douzi.org/blog
    - History -
    1.0 2004-02-01 Initial Version
    2.0 2004-02-01 Use PREG instead of STRCMP and cycles, SPEED UP!
    */
    preg_match_all("/[\x01-\x7f]|[\xc2-\xdf][\x80-\xbf]|\xe0[\xa0-\xbf][\x80-\xbf]|[\xe1-\xef][\x80-\xbf][\x80-\xbf]|\xf0[\x90-\xbf][\x80-\xbf][\x80-\xbf]|[\xf1-\xf7][\x80-\xbf][\x80-\xbf][\x80-\xbf]/", $str, $ar);

    if(func_num_args() >= 3) {
        $end = func_get_arg(2);
        if(count($ar[0])>$end){
            $str = join("",array_slice($ar[0],$start,$end)).$sss;
        }
        else{
            $str = $str;
        }
    }
    else{
        $str = join("",array_slice($ar[0],$start)).$sss;
    }
    $patt = "/(<[^>]*\.\.\.)/";
    preg_match_all($patt, $str, $matches);
    $replace_str = $matches[1][0];
    $str = str_replace($replace_str, "...", $str);

    return $str;
}


function __autoload($className)
{
    if (file_exists(CLASS_DIR . strtolower($className) . '.class.php')){
        require_once(CLASS_DIR. strtolower($className) . '.class.php');
    }
    else if (file_exists(CONTROLLER_DIR . strtolower($className) . '.class.php')){
        require_once(CONTROLLER_DIR . strtolower($className) . '.class.php');
    }
    else if (file_exists(MODEl_DIR . strtolower($className) . '.class.php')){
        require_once(MODEl_DIR . strtolower($className) . '.class.php');
    }
    else{
        /* Error Generation Code Here */
    }
}



//高亮关键字
function HighLight($Content,$KeyWords,$Num = '')
{
    $HighLightKeyWords = "<font color=red>$KeyWords</font>";
    $Content = str_replace($KeyWords,$HighLightKeyWords,$Content);
    if(!empty($Num)){
        $Content = CsubStr($Content,$Num);
    }
    return $Content;
}


/**
 * php DES解密函数
 *
 * @param string $key 密钥
 * @param string $encrypted 加密字符串
 * @return string
 */
function des_decode($key,$encrypted)
{
    $encrypted = base64_decode($encrypted);

    $td = mcrypt_module_open(MCRYPT_DES,'',MCRYPT_MODE_CBC,''); //使用MCRYPT_DES算法,cbc模式
    $iv = mcrypt_create_iv(mcrypt_enc_get_iv_size($td), MCRYPT_RAND);
    $ks = mcrypt_enc_get_key_size($td);

    mcrypt_generic_init($td, $key, $key);//初始处理
    $decrypted = mdecrypt_generic($td, $encrypted);//解密

    mcrypt_generic_deinit($td); //结束
    mcrypt_module_close($td);
    return   pkcs5_unpad($decrypted);
}
function pkcs5_unpad($text){
	$pad = ord($text{strlen($text)-1});
	if ($pad > strlen($text)) return $text;
	if (strspn($text, chr($pad), strlen($text) - $pad) != $pad) return $text;
	return substr($text, 0, -1 * $pad);
}
/**
 * php DES加密函数
 *
 * @param string $key 密钥
 * @param string $text 字符串
 * @return string
 */
function des_encode($key,$text){
    $y = pkcs5_pad($text);
    $td = mcrypt_module_open(MCRYPT_DES,'',MCRYPT_MODE_CBC,''); //使用MCRYPT_DES算法,cbc模式
    $ks = mcrypt_enc_get_key_size($td);

    mcrypt_generic_init($td, $key, $key);       //初始处理
    $encrypted = mcrypt_generic($td, $y);       //解密
    
    mcrypt_generic_deinit($td);       //结束
    mcrypt_module_close($td);
    return base64_encode($encrypted);
}
function pkcs5_pad($text,$block=8){
	$pad = $block - (strlen($text) % $block);
	return $text . str_repeat(chr($pad), $pad);
}




